new Vue({
	el: "body",
	data: {
		joblist: {},
		objectid: "",
		resume: true,
		hasdelivery: false,
		hascollection: false,
	},
	methods: {
		company: function() {
			location.href = "../companyDetail/companyDetail.html?companyName=" + set(this.joblist.companyName);
		},
		delivery: function() { //投递简历
			var date = new Date();
			var month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);
			var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
			var update = date.getFullYear() + "-" + month + "-" + day;
			var _this = this;
			var currentUser = AV.User.current();
			if(currentUser) { //判断是否登录
				var userid = currentUser.id; //获取用户id
				var query = new AV.Query('resume'); //获取简历表
				query.equalTo('userid', userid); //查询简历
				query.find().then(function(success) {
					console.log("简历查询成功");
					if(success.length == 0) { //没有简历
						alert("暂无简历，页面即将跳转");
						location.href = "../myResume/myResume.html";
					} else { //有简历，投递
						var Product = AV.Object.extend('delivery'); //投递记录表
						var product = new Product();

						product.set('objectid', _this.objectid); //职位的id
						product.set('positionName', _this.joblist.positionName); //职位名称
						product.set('companyName', _this.joblist.companyName); //公司名称
						console.log("有简历");
						product.set('update', update); //投递时间
						product.set('userid', userid); //用户ID
						product.save().then(function() {
							alert("添加成功");
							_this.hasdelivery = true;
						}, function(error) {
							alert("添加失败");
							alert(JSON.stringify(error));
						});
					}
				}).catch(function(error) {
					console.log(JSON.stringify(error));
					console.log("查询失败")
				});
			} else {
				location.href = "../loginAndReg/login.html";
			}
		},
		collection: function() { //收藏简历
			var date = new Date();
			var month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);
			var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
			var update = date.getFullYear() + "-" + month + "-" + day;
			var _this = this;
			var currentUser = AV.User.current();
			if(currentUser) {} else { //未登录，跳转到登录界面
				location.href = "../loginAndReg/login.html";
			}
			var userid = currentUser.id; //获取用户id
			var Product = AV.Object.extend('collection');
			var product = new Product();
			product.set('objectid', this.objectid); //职位的id
			product.set('positionName', this.joblist.positionName); //职位名称
			product.set('companyName', this.joblist.companyName); //公司名称
			product.set('update', update); //投递时间
			product.set('userid', userid); //用户ID

			product.save().then(function() {
				alert("添加成功");
				_this.hascollection = true;
			}, function(error) {
				alert("添加失败");
				alert(JSON.stringify(error));
			});
		}
	},
	created: function() {
		init();

		var _this = this;
		var objectid = get(geturlitem(location.search, "objectid")); //职位id
		var query = new AV.Query('position'); //获取职位表
		query.get(objectid).then(function(success) {
			console.log(success);
			_this.joblist = success.attributes;
			_this.objectid = success.id; //职位id
		}).catch(function(error) {
			console.log(JSON.stringify(error));
		});
		//判断用户简历是否已经投递该职位
		var currentUser = AV.User.current();
		if(currentUser) { //判断是否登录
			var userid = currentUser.id;
			//组合查询
			var delivery1 = new AV.Query("delivery"); //获取简历表
			delivery1.equalTo('objectid', objectid); //按职位id查询
			var delivery2 = new AV.Query("delivery"); //获取简历表
			delivery2.equalTo('userid', userid); //按用户名查询
			var delivery = AV.Query.and(delivery1, delivery2); //组合查询
			delivery.find().then(function(success) {
				console.log(success);
				if(success.length > 0) {
					_this.hasdelivery = true;
				}
			})
		}
		//判断用户简历是否已经收藏该职位
		var currentUser = AV.User.current();
		if(currentUser) { //判断是否登录
			var userid = currentUser.id;
			//组合查询
			var collection1 = new AV.Query("collection"); //获取简历表
			collection1.equalTo('objectid', objectid); //按职位id查询
			var collection2 = new AV.Query("collection"); //获取简历表
			collection2.equalTo('userid', userid); //按用户名查询
			var collection = AV.Query.and(collection1, collection2); //组合查询
			collection.find().then(function(success) {
				console.log(success);
				if(success.length > 0) {
					_this.hascollection = true;
				}
			})
		}
	}
})